圖書館管理系統我們之前做了有關成員的擴充功能,今天則是要來做借書的功能,並且追蹤圖書目前的狀況。
首先先來確定需要哪些欄位
圖書館會員借書(必須為會員才可以租借)
借書日期(預設今天)
誰借的書 (預設目前使用者)
借書清單,所需的書籍(一本OR多本)
另外借書:
再來是建立library_checkout模組,跟之前做library_member有些相似,,在library_app旁邊建立新的目錄。
先分別到__manifest__.py
{ "name": "Library Book Checkout",
"description": "Members can borrow books from the
library.",
"author": "Daniel Reis",
"depends": ["library_member"],
"data": [
"security/ir.model.access.csv",
"views/library_menu.xml",
"views/checkout_view.xml",
],
}
跟__init__.py
from . import models
from . import library_checkout
再新增models/library_checkout.py
from odoo import fields, models
class Checkout(models.Model):
_name = "library.checkout"
_description = "Checkout Request"
member_id = fields.Many2one(
"library.member",
required=True,
)
user_id = fields.Many2one(
"res.users",
"Librarian",
default=lambda s: s.env.user,
)
request_date = fields.Date(
default=lambda s: fields.Date.today(),
)
再來設置存取規則、選單項目和一些基本views,讓我們的module可以使用。
到security/ir.model.access.csv
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
checkout_user,Checkout User,model_library_checkout,library_app.library_group_user,1,1,1,1
views/library_menu.xml
<odoo>
<record id="action_library_checkout"
model="ir.actions.act_window">
<field name="name">Checkouts</field>
<field name="res_model">library.checkout</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="menu_library_checkout"
name="Checkout"
action="action_library_checkout"
parent="library_app.menu_library"
/>
</odoo>
views/checkout_view.xml
<odoo>
<record id="view_tree_checkout" model="ir.ui.view">
<field name="name">Checkout Tree</field>
<field name="model">library.checkout</field>
<field name="arch" type="xml">
<tree>
<field name="request_date" />
<field name="member_id" />
</tree>
</field>
</record>
<record id="view_form_checkout" model="ir.ui.view">
<field name="name">Checkout Form</field>
<field name="model">library.checkout</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="member_id" />
<field name="request_date" />
<field name="user_id" />
</group>
</sheet>
</form>
</field>
</record>
</odoo>
接下來安裝**-i跟更新-u**library_checkout,就能有基本需求。
另外對租借要怎麼管理,要來處理借書的訂單:
到models/library_checkout.py新增一對多的欄位
line_ids = fields.One2many(
"library.checkout.line",
"checkout_id",
string="Borrowed Books",
)
將新的model field 加入models/init.py
from . import library_checkout_line
models/library_checkout_line.py 新增以下指令
from odoo import api, exceptions, fields, models
class CheckoutLine(models.Model):
_name = "library.checkout.line"
_description = "Checkout Request Line"
checkout_id = fields.Many2one(
"library.checkout",
required=True,
)
book_id = fields.Many2one("library.book",
required=True)
note = fields.Char("Notes")
到security/ir.model.access.csv
checkout_line_user,Checkout Line User,model_library_checkout,library_app.library_group_user,1,1,1,1
新增views/checkout_view.xml,用notebook來顯示我們租借哪些書:
<notebook>
<page name="lines">
<field name="line_ids">
<tree editable="bottom">
<field name="book_id" />
<field name="note" />
</tree>
</field>
</page>
</notebook>